<?php
$pathFile = __FILE__;
$dir = dirname($pathFile);
$configFile = '../application/configs/application.ini';

if(!file_exists($dir .'/' . $configFile ))
{ 
    die("Doesn't existed config file.");
}

$config = parse_ini_file($dir .'/' . $configFile, true);

if(empty($config["database : production"]) || !is_array($config["database : production"]))
{
    die("Doesn't existed settings for database");
}

$host = $config["database : production"]["database.params.host"];
$user = $config["database : production"]["database.params.username"];
$password = $config["database : production"]["database.params.password"];
$db = $config["database : production"]["database.params.dbname"];


class mySQL {
    
    protected $host;
    protected $user;
    protected $password;
    protected $db;
    protected $link;
    
    public function setHost($host)
    {
        $this->host = trim($host);
    }
    
    public function getHost()
    {
        return $this->host;
    }
    
    public function setUser($user)
    {
        $this->user = trim($user);
    }
    
    public function getUser()
    {
        return $this->user;
    }
    
    public function setPassword($password)
    {
        $this->password = trim($password);
    }
    
    public function getPassword()
    {
        return $this->password;
    }

    public function setDatabase($db)
    {
        $this->db = trim($db);
    }

    public function getDatabase()
    {
        return $this->db;
    }

    public function query($query)
    {
        if($this->link)
        {
            @mysql_query($query);
        }
    }
    
    public function connect()
    {
        $this->link = @mysql_connect($this->host, $this->user, $this->password);
        
        if(!$this->link)
        {
            throw new Exception('Could not connect: ' . mysql_error());
        }

        if(!@mysql_select_db($this->db))
        {
            throw new Exception("Unable to select $this->db: " . mysql_error());
        }
    }
    
    public function disconnect()
    {
        if($this->link)
        {
           @mysql_close($this->link);
        }
    }
    
}
try {
    $mysql = new mySQL();
    $mysql->setHost($host);
    $mysql->setUser($user);
    $mysql->setPassword($password);
    $mysql->setDatabase($db);
    $mysql->connect();
	$mysql->query("INSERT INTO `config`(`name`, `value`) VALUES('page_teaser_size', '200');");
    $mysql->query("CREATE TABLE `shopping_config` (             
                   `name` varchar(100) NOT NULL,              
                   `value` varchar(700) NOT NULL default '',  
                   PRIMARY KEY  (`name`)                      
                 )");

    $mysql->query("CREATE TABLE `shopping_groups` (                        
                   `id` int(11) NOT NULL auto_increment,                 
                   `name` varchar(255) NOT NULL,                         
                   PRIMARY KEY  (`id`)                                   
                 )");

    $mysql->query("CREATE TABLE `shopping_products` (
                     `id` int(11) NOT NULL auto_increment,
                     `name` varchar(255) NOT NULL,
                     `item_id` varchar(25) NOT NULL,
                     `price` varchar(25) NOT NULL,
                     `photo` varchar(255) NOT NULL,
                     `brand` varchar(255) default '',
                     `desc` varchar(500) default '',
                     `options` varchar(400) default '',
                     `related` varchar(300) default '',
                     `page_id` int(11) default '0',
                     PRIMARY KEY  (`id`)
                   )");

    $mysql->query("CREATE TABLE `shopping_products_groups` (
                            `id_product` int(11) NOT NULL,
                            `id_group` int(11) NOT NULL,
                            UNIQUE KEY `product_group_unique` (`id_product`,`id_group`),
                            KEY `id_product` (`id_product`),
                            KEY `id_group` (`id_group`)
                          ) ");

	$mysql->query("CREATE TABLE `shopping_quote` (
                  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                  `name` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
                  `content` TEXT COLLATE utf8_unicode_ci,
                  `status` ENUM('new','sent','sold','lost') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'new',
                  `int_msg` TEXT COLLATE utf8_unicode_ci,
                  `sc_id` INT(10) UNSIGNED NOT NULL,
                  `date` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
                  PRIMARY KEY (`id`)
                ) ENGINE=INNODB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ");

	$mysql->query("CREATE TABLE `shopping_usercart` (
                     `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                     `name` VARCHAR(64) COLLATE utf8_unicode_ci NOT NULL,
                     `email` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
                     `address1` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `address2` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `country` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `state` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `city` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `zip` VARCHAR(25) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `phone` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `comments` TEXT COLLATE utf8_unicode_ci,
                     `data` TEXT COLLATE utf8_unicode_ci,
                     `date` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
                     PRIMARY KEY (`id`)
                   ) ENGINE=INNODB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
    
	$mysql->query("ALTER TABLE `page` ADD COLUMN `targeted_key_phrase` VARCHAR(255) CHARSET utf8 COLLATE utf8_unicode_ci NULL AFTER `silo_id`;");
	$mysql->query("ALTER TABLE `category` CHANGE `name` `name` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL ;");
	$mysql->query("ALTER TABLE `plugin` ADD COLUMN `cache` ENUM('0','1') DEFAULT '0';");
	$mysql->query("UPDATE `container` SET `container`.`container_type_id` = 2 WHERE `container`.`page_id` = 0 AND `container`.`container_type_id` = 1");
	$mysql->query("CREATE TABLE `password_recovering` (
          `link` varchar(100) character set utf8 NOT NULL default '',
          `time` varchar(255) character set utf8 NOT NULL
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
	$mysql->query("INSERT INTO `config` VALUES('page_teaser_size',200);");
	$mysql->query("ALTER TABLE `category` CHANGE `name` `name` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ;");
    /*$mysql->query("DROP TABLE IF EXISTS `thesaurus_en`;");
   $mysql->query("DROP TABLE IF EXISTS `role`");
   $mysql->query("CREATE TABLE `role` (
        `id` int(10) unsigned NOT NULL auto_increment,
        `name` varchar(45) default NULL,
        PRIMARY KEY  (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8");
    $mysql->query("insert  into `role`(`id`,`name`) values (1,'admin'),(2,'user'),(3,'superadmin')");
	$mysql->query("CREATE TABLE `seo_data`( `page_id` INT UNSIGNED , `seo_top` LONGTEXT CHARSET utf8 COLLATE utf8_general_ci , `seo_bottom` LONGTEXT CHARSET utf8 COLLATE utf8_general_ci );");
	$mysql->query("ALTER TABLE `seo_data` ADD UNIQUE `page_id` (`page_id`);");
	$mysql->query("
		CREATE TABLE `silo` (
			`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
			`name` varchar(65) DEFAULT NULL,
			PRIMARY KEY (`id`)
		) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
	");
	$mysql->query("ALTER TABLE `page` ADD COLUMN `silo_id` INT UNSIGNED NULL AFTER `seosamba_rule_id`;");
    $mysql->query("CREATE TABLE `forms` (
          `id` int(10) unsigned NOT NULL auto_increment,
          `name` varchar(64) NOT NULL,
          `code` text NOT NULL,
          `email` varchar(64) NOT NULL,
          `successMessage` text NOT NULL,
          `errorMessage` text NOT NULL,
          `emailFrom` varchar(64) NOT NULL,
          `emailMessage` text NOT NULL,
          `useSMTP` enum('0','1') NOT NULL default '0',
          PRIMARY KEY  (`id`)
        ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
    $mysql->query("ALTER TABLE `deeplink` ADD COLUMN `ext_ws_id` INT UNSIGNED NULL AFTER `nofollow`;");
    $mysql->query("ALTER TABLE `silo` ADD COLUMN `as_cat` ENUM('0','1') CHARSET utf8 COLLATE utf8_general_ci DEFAULT '0' NULL AFTER `name`;");
	$mysql->query("CREATE TABLE `whitelabel_services`( `cpanel_logo` VARCHAR(255) CHARSET utf8 COLLATE utf8_general_ci , `services_content` TEXT CHARSET utf8 COLLATE utf8_general_ci );");
    $mysql->query("ALTER TABLE `comment` ADD COLUMN publish TINYINT(1) DEFAULT 1;");
	$mysql->query("alter table `comment` add column user_email varchar(60) default '';");
	$mysql->query("alter table `comment` add column abuse tinyint(1) default 0;");
	$mysql->query("alter table `comment` add column user_sign varchar(40) default '';");
	$mysql->query("alter table `comment` add column user_website varchar(40) default '';");
	$mysql->query("alter table `comment` add column rank tinyint(3) default 0;");
	$mysql->query("alter table `comment` add column topic varchar(60) default '';");
	$mysql->query("alter table `comment` add column keyword varchar(60) default '';");
	$mysql->query("alter table `comment` add column notify tinyint(1) default 0;");*/
	$mysql->disconnect();
}
catch (Exception $e)
{
    echo $e->getMessage();
}


?>